package com.ggb.reactive.config

import com.ggb.reactive.handler.WebSocketHandler
import org.springframework.context.annotation.Bean
import org.springframework.context.annotation.Configuration
import org.springframework.web.reactive.HandlerMapping
import org.springframework.web.reactive.config.EnableWebFlux
import org.springframework.web.reactive.config.WebFluxConfigurer
import org.springframework.web.reactive.handler.SimpleUrlHandlerMapping

@Configuration
@EnableWebFlux
class WebSocketConfig(val webSocketHandler: WebSocketHandler) : WebFluxConfigurer {

    @Bean
    fun webSocketHandlerMapping(): HandlerMapping {
        val mapping = SimpleUrlHandlerMapping()
        mapping.urlMap = mapOf("/ws/{sid}" to webSocketHandler)
        mapping.order = -1 // 需要保证 WebSocketHandler 的优先级高于其他 Handler
        return mapping
    }
}
